package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customer/list";
    }

    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //设置客户状态为潜在客户状态
        qo.setStatus(Customer.STATUS_COMMON);
        //判断只有当前登录用户是管理员或者是市场经理才可以看到所有的潜在客户,否则只可以看到自己的客户
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            //获取当前登录用户的对象
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //获取全部的销售人员(市场经理或者市场专员)
        List<Employee> sellers = employeeService.selectByRoleSn("Market_Manager","Market");
        model.addAttribute("sellers", sellers);

        //查询所有的客户工作(工作下拉框)
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByDicSn("job");
        model.addAttribute("jobs", jobs);
        //查询所有的客户来源(来源下拉框)
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByDicSn("source");
        model.addAttribute("sources", sources);
        //查询所有的交流方式(共享到前端交流方式下拉框中)
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByDicSn("communicationMethod");
        model.addAttribute("ccts", ccts);
        return "customer/potentialList";
    }

    @RequiresPermissions(value = {"customer:poollList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //设置客户状态为潜在客户状态
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //获取全部的销售人员(市场经理或者市场专员)
        List<Employee> sellers = employeeService.selectByRoleSn("Market_Manager","Market");
        model.addAttribute("sellers", sellers);

        return "customer/poolList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }

    /**
     * 检查客户的电话是否存在
     * @param id
     * @param tel
     * @return
     */
    @RequestMapping("/checkTel")
    @ResponseBody
    public HashMap checkTel(Long id, String tel){
        HashMap<Object, Object> map = new HashMap<>();
        //根据页面传过来的客户id和客户电话判断客户电话是否存在
        //首先根据id判断当前操作是新增还是编辑操作
        if (id != null) {
            //不为空表示为编辑操作,根据id查询当前客户的电话
            String NowTel = customerService.get(id).getTel();
            //判断是否和页面传过来的相同
            if (tel.equals(NowTel)){
                //如果相同表示未修改,可以放行
                map.put("valid",true);
                return map;
            }else {
                //否则判断页面传过来的Tel是否存在
                Customer customer = customerService.selectByTel(tel);
                map.put("valid",customer==null);
                return map;
            }
        }else{
            //否则判断页面传过来的Tel是否存在
            Customer customer = customerService.selectByTel(tel);
            map.put("valid",customer==null);
            return map;
        }

    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id, Integer status){
        //将页面传过来的客户的状态进行修改
        customerService.updateStatus(id,status);
        return new JsonResult();
    }




}
